package com.example.it.looker.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.it.looker.R;
import com.example.it.looker.model.entity.Image;
import com.example.it.looker.view.BaseRecyclerAdapter;

import java.util.List;

/**
 * Created by 鲁迅认识的那只猹 on 9/5/2017 3:18 PM.
 * Emial 1258730808@qq.com
 * Desc:
 */

public class ImageRecyclerAdapter extends BaseRecyclerAdapter<Image> {

    /**
     * 构造函数
     *
     * @param context    上下文对象
     * @param dataSource 所需数据
     * @param resId      Item的布局资源Id
     */
    public ImageRecyclerAdapter(Context context, List<Image> dataSource, int resId) {
        super(context, dataSource, resId);
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == HEADER)
            return new ImageRecyclerViewHolder(getHeaderView());
        else if (viewType == FOOTER)
            return new ImageRecyclerViewHolder(getFooterView());
        else {
            View view = LayoutInflater.from(getContext()).inflate(getResId(), null);
            return new ImageRecyclerViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (isHeader(position)) return;
        if (isFooter(position)) return;
        int actualPosition = getActualPosition(position);
        Image image = getDataSource().get(actualPosition);
        ImageRecyclerViewHolder imageRecyclerViewHolder = (ImageRecyclerViewHolder) holder;
        Glide.with(getContext()).load(image.getUrl()).into(imageRecyclerViewHolder.iv);

    }

    public class ImageRecyclerViewHolder extends BaseRecyclerAdapter.RecyclerViewHolder {

        ImageView iv;

        public ImageRecyclerViewHolder(View itemView) {
            super(itemView);
            iv = (ImageView) itemView.findViewById(R.id.ivPhoto);
        }
    }
}
